            
class BookmarkController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

	def list = {
		if(!params.max) params.max = "10"
		if(!params.offset) params.offset = "0"
		if(!params.sort) params.sort = "id"
		if(!params.order) params.order = "asc"

		def hql
		
		hql = "select distinct b.tag from Bookmark b where b.user.username=:username"
		def tags = Bookmark.executeQuery(hql, [username:session.user.username])
		
		def tag = params["tag"]
		if( tag == null && tags.size() > 0) {
			tag = tags[0]
		}

		def bookmarks
		def count
		if (tag == null) {
			hql = "from Bookmark b where b.user.username=:username order by " + params.sort + " " + params.order
			bookmarks = Bookmark.executeQuery(hql, [username:session.user.username, max:Integer.parseInt(params.max), offset:Integer.parseInt(params.offset)])
			hql = "from Bookmark b where b.user.username=:username "
			count = Bookmark.executeQuery(hql, [username:session.user.username]).size()
		} else {
			hql = "from Bookmark b where b.user.username=:username and b.tag=:tag order by " + params.sort + " " + params.order
			bookmarks = Bookmark.executeQuery(hql, [username:session.user.username, tag:tag, max:Integer.parseInt(params.max), offset:Integer.parseInt(params.offset)])
			hql = "from Bookmark b where b.user.username=:username and b.tag=:tag "
			count = Bookmark.executeQuery(hql, [username:session.user.username, tag:tag]).size()
		}
		
		[ bookmarkList: bookmarks, count: count, tagList: tags, tag: tag ]
	}

    def show = {
        def bookmark = Bookmark.get( params.id )

        if(!bookmark) {
            flash.message = "Bookmark not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ bookmark : bookmark ] }
    }

    def delete = {
        def bookmark = Bookmark.get( params.id )
        if(bookmark) {
            bookmark.delete()
            flash.message = "Bookmark ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "Bookmark not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def bookmark = Bookmark.get( params.id )

        if(!bookmark) {
            flash.message = "Bookmark not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ bookmark : bookmark ]
        }
    }

    def update = {
        def bookmark = Bookmark.get( params.id )
        if(bookmark) {
            bookmark.properties = params
            if(!bookmark.hasErrors() && bookmark.save()) {
                flash.message = "Bookmark ${params.id} updated"
                redirect(action:show,id:bookmark.id)
            }
            else {
                render(view:'edit',model:[bookmark:bookmark])
            }
        }
        else {
            flash.message = "Bookmark not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def bookmark = new Bookmark()
        bookmark.properties = params
        bookmark.url = request.getQueryString()
        return ['bookmark':bookmark]
        //def url = request.getQueryString()
		//render( view: 'create', model:[url: url])
    }

    def save = {
        def bookmark = new Bookmark(params)
        if(!bookmark.hasErrors() && bookmark.save()) {
            flash.message = "Bookmark ${bookmark.id} created"
            redirect(action:show,id:bookmark.id)
        }
        else {
            render(view:'create',model:[bookmark:bookmark])
        }
    }
	
    def createLink = {
	}
	
	def openLink = {
		def bookmark = Bookmark.get(params.id)
        if(!bookmark) {
            flash.message = "Bookmark not found with id ${params.id}"
            redirect(action:list)
        } else {
            render(view: 'openLink', model: [bookmark:bookmark])
        }
	}
}